package cn.zxd.member.claims.dao;

import cn.zxd.member.claims.vo.*;

import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;

/**
 * Created by DaoCreator (daoJdbc.jt)
 */
public class ClaimsJdbcDao implements ClaimsDao{
    private static Logger logger = Logger.getLogger(ClaimsJdbcDao.class);
    private Dao dao;

    public ClaimsJdbcDao(Dao dao){
        logger.debug("Dao :"+this.getClass().getName());
        this.dao = dao;
    }

    //#appendbegin
    //==============Claims==============
    String Claims_QUERY_SQL = "select t.*, c.name as custName,  car.carNo, " +
            "u.username as createUserName from Claims t " +
            "left join customer c on c.sn = t.custSn " +
            "left join customer_car car on car.sn = t.custCarSn " +
            "left join sysuser u on u.sn = t.createUser ";

    private String createClaimsFilter(Map<String, Object> param) {
        //to add filter,modify var filter
        String filter = " where 1=1 ";
        if (param.get("sn") != null && !param.get("sn").equals("")) {
            filter += " and t.sn = @sn ";
        }
        if (param.get("custSn") != null && !param.get("custSn").equals("")) {
            filter += " and t.custSn = @custSn ";
        }
        if (param.get("carNo") != null && !param.get("carNo").equals("")) {
            filter += " and t.carNo = @carNo ";
        }
        if (param.get("custCarSn") != null && !param.get("custCarSn").equals("")) {
            filter += " and t.custCarSn = @custCarSn ";
        }
        if (param.get("paNo") != null && !param.get("paNo").equals("")) {
            filter += " and t.paNo = @paNo ";
        }
        if (param.get("enPolicyNo") != null && !param.get("enPolicyNo").equals("")) {
            filter += " and t.enPolicyNo = @enPolicyNo ";
        }
        if (param.get("enPolicySn") != null && !param.get("enPolicySn").equals("")) {
            filter += " and t.enPolicySn = @enPolicySn ";
        }
        if (param.get("buPolicyNo") != null && !param.get("buPolicyNo").equals("")) {
            filter += " and t.buPolicyNo = @buPolicyNo ";
        }
        if (param.get("buPolicySn") != null && !param.get("buPolicySn").equals("")) {
            filter += " and t.buPolicySn = @buPolicySn ";
        }
        if (param.get("accDate") != null && !param.get("accDate").equals("")) {
            filter += " and t.accDate = @accDate ";
        }
        if (param.get("reportPerson") != null && !param.get("reportPerson").equals("")) {
            filter += " and t.reportPerson = @reportPerson ";
        }
        if (param.get("reportTel") != null && !param.get("reportTel").equals("")) {
            filter += " and t.reportTel = @reportTel ";
        }
        if (param.get("accDetail") != null && !param.get("accDetail").equals("")) {
            filter += " and t.accDetail = @accDetail ";
        }
        if (param.get("repairPlace") != null && !param.get("repairPlace").equals("")) {
            filter += " and t.repairPlace = @repairPlace ";
        }
        if (param.get("fee") != null && !param.get("fee").equals("")) {
            filter += " and t.fee = @fee ";
        }
        if (param.get("state") != null && !param.get("state").equals("")) {
            filter += " and t.state = @state ";
        }
        if (param.get("duty") != null && !param.get("duty").equals("")) {
            filter += " and t.duty = @duty ";
        }
        if (param.get("memo") != null && !param.get("memo").equals("")) {
            filter += " and t.memo = @memo ";
        }

        return filter;
    }

    private String createClaimsOrderBy(String orderBy) {
        if (orderBy != null && !orderBy.equals(""))
            return " order by " + orderBy;
        else return "";
    }

    public void insertClaims(Claims vo) {
        dao.insert(vo);
    }

    public void updateClaims(Claims vo) {
        dao.update(vo);
    }

    public void deleteClaims(Claims vo) {
        dao.delete(vo);
    }

    public void deleteClaimsByPrimaryKey(String sn ) {
        dao.delete(Claims.class, sn);
    }

    public void deleteClaimsByParam(Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i<param.length; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        deleteClaimsByParam(map);
    }

    public void deleteClaimsByParam(Map<String, Object> param) {
        if (param == null) {
            param = new HashMap<String, Object>();
        }
        String filter = createClaimsFilter(param);
        //don't delete when no filter
        if (!filter.trim().toLowerCase().equals("where 1=1")) {
            String sql = "delete from Claims " + filter.replace("t.", "");
            Sql s = Sqls.create(sql);
            s.params().putAll(param);
            dao.execute(s);
        } 
    }

    public ClaimsSO getClaimsByPrimaryKey(String sn ) {
        String sql= Claims_QUERY_SQL + " where t.sn = @sn  ";
        Sql s = Sqls.create(sql);
        //todo:
        s.params().set("sn", sn);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(ClaimsSO.class));
        dao.execute(s);
        return s.getObject(ClaimsSO.class);
    }

    public int getClaimsCountByPrimaryKey(String sn ) {
        String sql="select count(sn) from Claims t where t.sn = @sn   ";
        Sql s = Sqls.create(sql);
        //todo:
        s.params().set("sn", sn);
        s.setCallback(Sqls.callback.integer());
        dao.execute(s);
        return s.getInt();
    }

    public int getClaimsCountByParam(Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i<param.length; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getClaimsCountByParam(map);
    }

    public int getClaimsCountByParam(Map<String, Object> param) {
        if (param == null) {
            param = new HashMap<String, Object>();
        }
        //to add fileter modify createClaimsFilter
        String filter = createClaimsFilter(param);
        String sql = "select count(sn) from Claims t " + filter;
        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.integer());
        dao.execute(s);
        return s.getInt();
    }

    public List<ClaimsSO> getClaimsListByParam(String orderBy, Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i < param.length ; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getClaimsListByParam(orderBy, map);
    }

    public List<ClaimsSO> getClaimsListByParam(int page, int count, String orderBy, Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i < param.length ; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getClaimsListByParam(orderBy, map, page, count);
    }

    public List<ClaimsSO> getClaimsListByParam(String orderBy, Map<String, Object> param) {
        if (param == null) {
            param = new HashMap<String, Object>();
        }
        //to add filter modify createClaimsFilter
        String filter = createClaimsFilter(param);
        String sql = "select * from (" + Claims_QUERY_SQL + filter + ") dataall " + createClaimsOrderBy(orderBy);
        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(ClaimsSO.class));
        dao.execute(s);
        return s.getList(ClaimsSO.class);
    }

    public List<ClaimsSO> getClaimsListByParam(String orderBy, Map<String, Object> param, int page, int count) {
        if (param == null) {
            param = new HashMap<String, Object>();
        }
        param.put("row0", (page - 1) * count);
        param.put("row1", count);

        //to add filter modify createClaimsFilter
        String filter = createClaimsFilter(param);
        String sql =  "select * from (" + Claims_QUERY_SQL + filter + ") dataall " + createClaimsOrderBy(orderBy) + " limit @row0, @row1";

        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(ClaimsSO.class));
        dao.execute(s);
        return s.getList(ClaimsSO.class);


    }

    public List<ClaimsSO> getAllClaimsList(String orderBy){
	    String sql = Claims_QUERY_SQL;
		Sql s = Sqls.create("select * from (" + sql + ") dataall " + createClaimsOrderBy(orderBy));
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(ClaimsSO.class));
        dao.execute(s);
        return s.getList(ClaimsSO.class);
    }
    //==============end of Claims==============
    //#appendend
}
